
<!DOCTYPE HTML>
<html lang="zh-cn" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>引入服务层 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Ajax-interface.html" />
    
    
    <link rel="prev" href="URL-is-soul-of-SPA.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="輸入並搜尋" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    总览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Getting-started.html">
            
                <a href="../Getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../Features.html">
            
                <a href="../Features.html">
            
                    
                    技术栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../Structure.html">
            
                <a href="../Structure.html">
            
                    
                    目录结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    深入了解
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="State-management.html">
            
                <a href="State-management.html">
            
                    
                    谈状态管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="URL-is-soul-of-SPA.html">
            
                <a href="URL-is-soul-of-SPA.html">
            
                    
                    URL 是单页应用的精华
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3" data-path="Service-layer.html">
            
                <a href="Service-layer.html">
            
                    
                    引入服务层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Ajax-interface.html">
            
                <a href="Ajax-interface.html">
            
                    
                    Ajax 接口化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Configuration.html">
            
                <a href="Configuration.html">
            
                    
                    配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="CORS-and-Proxy.html">
            
                <a href="CORS-and-Proxy.html">
            
                    
                    跨域与代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="Best-practice.html">
            
                <a href="Best-practice.html">
            
                    
                    最佳实践
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <a target="_blank" href="https://github.com/kenberkeley/vue-demo/issues">
            
                    
                    问题反馈（issues）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/kenberkeley/vue-demo/releases">
            
                    
                    版本迭代（releases）
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本書使用 GitBook 釋出
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >引入服务层</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#xA7;-&#x5F15;&#x5165;&#x670D;&#x52A1;&#x5C42;">&#xA7; &#x5F15;&#x5165;&#x670D;&#x52A1;&#x5C42;</h1>
<p>&#x670D;&#x52A1;&#x5C42;&#x7684;&#x4E3B;&#x8981;&#x529F;&#x80FD;&#x662F;&#xFF1A;<strong>&#x5C01;&#x88C5;&#x597D;&#x4E0E;&#x540E;&#x7AEF; API &#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#x7684;&#x8BF7;&#x6C42;&#x7C7B;</strong>&#x3002;<br>&#x4F8B;&#x5982;&#xFF0C;&#x7528;&#x6237;&#x767B;&#x5F55;&#x7684; API &#x4E3A; <code>/auth/login</code>&#xFF0C;<br>&#x90A3;&#x4E48; <code>src/services/authService.js</code> &#x8FD9;&#x4E2A;&#x7C7B;&#x4E2D;&#x5C31;&#x5BF9;&#x5E94;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <code>login</code> &#x7684;&#x51FD;&#x6570;&#xFF0C;<br>&#x53EA;&#x9700;&#x8981;&#x8C03;&#x7528; <code>authService.login({ &#x5E10;&#x53F7;, &#x5BC6;&#x7801; })</code> &#x5373;&#x53EF;&#x5B9E;&#x73B0;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x8FD9;&#x6837;&#x505A;&#x7684;&#x597D;&#x5904;&#x662F;&#xFF1A;<strong>&#x8F7B;&#x91CF;&#x5316;&#x7EC4;&#x4EF6;&#xFF0C;&#x7EDF;&#x4E00;&#x7BA1;&#x7406; XHR &#x8BF7;&#x6C42;&#xFF0C;&#x63D0;&#x9AD8;&#x4EE3;&#x7801;&#x590D;&#x7528;&#xFF0C;&#x65B9;&#x4FBF; mock</strong>&#xFF0C;<br>&#x907F;&#x514D;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x5206;&#x522B;&#x5B9E;&#x73B0;&#x8BF7;&#x6C42;&#x800C;&#x5BFC;&#x81F4;&#x7BA1;&#x7406;&#x4E0A;&#x7684;&#x6DF7;&#x4E71;&#xFF08;&#x5BF9;&#x65E5;&#x540E;&#x7684;&#x91CD;&#x6784;&#x4E5F;&#x4E0D;&#x53CB;&#x597D;&#xFF09;&#x3002;<br>&#x800C;&#x4E14;&#xFF0C;<strong>&#x524D;&#x7AEF;&#x7684;&#x670D;&#x52A1;&#x4E0E;&#x540E;&#x7AEF;&#x7684; API &#x4E00;&#x4E00;&#x5BF9;&#x5E94;</strong>&#xFF0C;&#x5728;&#x7406;&#x89E3;&#x4E0A;&#x4E5F;&#x4F1A;&#x53D8;&#x5F97;&#x66F4;&#x52A0;&#x5BB9;&#x6613;&#x3002;</p>
<p>&#x66F4;&#x91CD;&#x8981;&#x7684;&#x662F;&#xFF0C;&#x670D;&#x52A1;&#x5C42;&#x4E0E; Vue &#x6280;&#x672F;&#x6808;&#x5E76;&#x6CA1;&#x6709;&#x592A;&#x5927;&#x5173;&#x7CFB;&#xFF08;&#x524D;&#x63D0;&#x662F;&#x60A8;&#x6CA1;&#x6709;&#x7528; <a href="https://github.com/pagekit/vue-resource" target="_blank"><code>vue-resource</code></a> &#x8FD9;&#x7C7B;&#x76F8;&#x5BF9;&#x9E21;&#x808B;&#x7684; Ajax &#x5E93;&#xFF09;&#x3002;<br>&#x5F80;&#x540E;&#x6539;&#x7528;&#x5176;&#x4ED6;&#x6280;&#x672F;&#x6808;&#xFF08;React &#x7B49;&#xFF09;&#x65F6;&#xFF0C;&#x670D;&#x52A1;&#x5C42;&#x53EF;&#x76F4;&#x63A5;&#x590D;&#x5236;&#x8FC7;&#x53BB;&#xFF0C;&#x6BCB;&#x987B;&#x6539;&#x52A8;&#x4EFB;&#x4F55;&#x4EE3;&#x7801;&#x3002;</p>
<blockquote>
<p><a href="https://github.com/kenberkeley/react-demo" target="_blank">React Demo</a> &#x5C31;&#x662F;&#x76F4;&#x63A5;&#x590D;&#x5236;&#x672C; demo &#x7684; <code>services/</code> &#x76EE;&#x5F55;</p>
</blockquote>
<hr>
<p>&#x670D;&#x52A1;&#x5C42;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x5999;&#x7528;&#xFF1A;<strong>&#x7528;&#x4E8E;&#x7F13;&#x5B58;&#x76F8;&#x5173;&#x7684;&#x6570;&#x636E;</strong>&#x3002;<br>&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;&#x9009;&#x62E9;&#x7528;&#x6237;&#x7684;&#x4E0B;&#x62C9;&#x6846;&#x7EC4;&#x4EF6;&#xFF0C;&#x91CC;&#x9762;&#x7684;&#x6570;&#x636E;&#x662F;&#x52A8;&#x6001;&#x83B7;&#x53D6;&#x7684;&#xFF08;&#x5047;&#x8BBE; API &#x4E3A; <code>GET /authors</code>&#xFF09;&#xFF0C;<br>&#x82E5;&#x4E0D;&#x628A;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x7F13;&#x5B58;&#x4E0B;&#x6765;&#xFF0C;&#x90A3;&#x4E48;&#x6BCF;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x8BE5;&#x7EC4;&#x4EF6;&#xFF0C;&#x90FD;&#x4F1A;&#x53D1;&#x51FA;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x3002;<br>&#x5C24;&#x5176;&#x662F;&#x8BE5;&#x7EC4;&#x4EF6;&#x6781;&#x5176;&#x5E38;&#x7528;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x66F4;&#x4F1A;&#x9020;&#x6210;&#x5F88;&#x591A;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x8D44;&#x6E90;&#x6D88;&#x8017;&#x3002;</p>
<p>&#x4E8E;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x4E3A;&#x5176;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#xFF0C;&#x547D;&#x540D;&#x4E3A; <code>authorService</code>&#xFF0C;&#x91CC;&#x9762;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A; <code>getOptions</code> &#x65B9;&#x6CD5;&#xFF0C;<br>&#x5728;&#x7EC4;&#x4EF6;&#x521D;&#x59CB;&#x5316;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B50;&#x64CD;&#x4F5C;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span> (authorService.options) {
  <span class="hljs-comment">// &#x5B58;&#x5728;&#x7F13;&#x5B58;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x7F13;&#x5B58;</span>
  <span class="hljs-keyword">this</span>.options = authorService.options
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">// &#x4E0D;&#x5B58;&#x5728;&#x7F13;&#x5B58;&#xFF0C;&#x5219;&#x52A8;&#x6001;&#x83B7;&#x53D6;</span>
  authorService.getOptions().then(options =&gt; {
    <span class="hljs-keyword">this</span>.options = authorService.options = options <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x7F13;&#x5B58;</span>
  })
}
</code></pre>
<p>&#x7531;&#x6B64;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;<strong>&#x9875;&#x9762;&#x7EA7;&#x522B;</strong>&#x7684;&#x7F13;&#x5B58;&#xFF0C;&#x5F3A;&#x5236;&#x5237;&#x65B0;&#x5373;&#x53EF;&#x83B7;&#x53D6;&#x6700;&#x65B0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6BCB;&#x987B;&#x7279;&#x610F;&#x53BB;&#x7EF4;&#x62A4;&#x8BE5;&#x7F13;&#x5B58;&#x3002;</p>
<blockquote>
<p>&#x8BD5;&#x60F3;&#xFF0C;&#x82E5;&#x4F7F;&#x7528; LocalStorage / SessionStorage &#x7F13;&#x5B58;&#xFF0C;&#x8FD8;&#x8981;&#x8003;&#x8651;&#x517C;&#x5BB9;&#x6027;&#x3001;&#x6570;&#x636E;&#x66F4;&#x65B0;&#x3001;&#x6709;&#x6548;&#x6027;&#x3001;&#x5B89;&#x5168;&#x6027;&#x7B49;&#x8BF8;&#x591A;&#x95EE;&#x9898;</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="URL-is-soul-of-SPA.html" class="navigation navigation-prev " aria-label="Previous page: URL 是单页应用的精华">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Ajax-interface.html" class="navigation navigation-next " aria-label="Next page: Ajax 接口化">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"引入服务层","level":"1.5.3","depth":2,"next":{"title":"Ajax 接口化","level":"1.5.4","depth":2,"path":"development/Ajax-interface.md","ref":"development/Ajax-interface.md","articles":[]},"previous":{"title":"URL 是单页应用的精华","level":"1.5.2","depth":2,"path":"development/URL-is-soul-of-SPA.md","ref":"development/URL-is-soul-of-SPA.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-cn","gitbook":"*"},"file":{"path":"development/Service-layer.md","mtime":"2017-02-07T02:44:13.054Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-02-07T06:28:34.135Z"},"basePath":"..","book":{"language":"zh-cn"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

